package 学生成绩查询管理系统.界面;

import 学生成绩查询管理系统.JDBC.JDBCUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Login extends JFrame {
    JPanel root;
    JLabel userNameLabel, passWordLabel, Title;
    JTextField userTextField;
    JPasswordField passWordTextField;
    JButton enterButton;
    JRadioButton rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;

//    String path = "C:\\Users\\23513\\Desktop\\学生成绩管理系统\\";

    public Login() {

        initJframe();

        initimage();


        addActionListener(userTextField, passWordTextField, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2, enterButton, this);
        setVisible(true);

    }

    private void addActionListener(JTextField userTextField, JPasswordField passWordTextField, JRadioButton rdbtnNewRadioButton, JRadioButton rdbtnNewRadioButton1, JRadioButton rdbtnNewRadioButton2, JButton enterButton, Login login) {

        //单选按钮互斥
        rdbtnNewRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (rdbtnNewRadioButton.isSelected()) {
                    rdbtnNewRadioButton_1.setSelected(false);
                    rdbtnNewRadioButton_2.setSelected(false);
                }
            }
        });
        rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (rdbtnNewRadioButton_1.isSelected()) {
                    rdbtnNewRadioButton.setSelected(false);
                    rdbtnNewRadioButton_2.setSelected(false);
                }
            }
        });
        rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (rdbtnNewRadioButton_2.isSelected()) {
                    rdbtnNewRadioButton_1.setSelected(false);
                    rdbtnNewRadioButton.setSelected(false);
                }
            }
        });
        //登录按钮

         enterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String Sno = userTextField.getText();
                String Passwd = passWordTextField.getText();
                if (Sno.length() == 0 || Passwd.length() == 0) {
                    JOptionPane.showMessageDialog(null, "亲，请检查账号或密码的合法性！", "登录信息提示", JOptionPane.WARNING_MESSAGE);
                    return;
                } else if (!rdbtnNewRadioButton.isSelected() && !rdbtnNewRadioButton_1.isSelected() && !rdbtnNewRadioButton_2.isSelected()) {
                    JOptionPane.showMessageDialog(null, "亲，请选择登录身份！", "登录信息提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
//管理员、学生、教师登录判断
                if (rdbtnNewRadioButton_2.isSelected()) {
                    JDBCUtils admin = new JDBCUtils();
                    boolean isValidLogin = admin.admincheckLogin(Sno, Passwd);

                    if (isValidLogin) {
                        // 登录成功，关闭当前窗口
                        dispose();
                        new StudentSystem();
                        // 打开新界面

                    } else {
                        JOptionPane.showMessageDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
                if (rdbtnNewRadioButton_1.isSelected()) {
                    JDBCUtils teacher = new JDBCUtils();
                    boolean isValidLogin = teacher.teachercheckLogin(Sno, Passwd);

                    if (isValidLogin) {
                        // 登录成功，关闭当前窗口
                        dispose();
                        new TeacherFrame();

                        // 打开新界面

                    } else {
                        JOptionPane.showMessageDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
                if (rdbtnNewRadioButton.isSelected()) {
                    JDBCUtils student = new JDBCUtils();
                    boolean isValidLogin = student.studentcheckLogin(Sno, Passwd);
                    if (isValidLogin) {
                        // 登录成功，关闭当前窗口
                        dispose();
                        new StudentFrame();
                        // 打开新界面

                    } else {
                        JOptionPane.showMessageDialog(null, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }

            }
        });
    }


    private void initJframe() {
        this.setTitle("学生成绩查询管理系统");
        root = new JPanel(new BorderLayout()); // 使用BorderLayout布局管理器
        setContentPane(root);
        Font centerFont = new Font("正楷", Font.PLAIN, 15);
        Font FFont = new Font("华文行楷", Font.PLAIN, 28);

        //标题
        Title = new JLabel("学生成绩查询管理系统", JLabel.CENTER);
        Title.setBounds(52 + 232, 50, 300, 100);
        root.add(Title);
        Title.setFont(FFont);
        //用户名标签
        userNameLabel = new JLabel("账  号:");
        userNameLabel.setBounds(52 + 280, 33 + 130, 54, 13);
        root.add(userNameLabel);
        userNameLabel.setFont(centerFont);
        //用户名文本框
        userTextField = new JTextField(12);
        userTextField.setBounds(116 + 280, 30 + 130, 139, 21);
        root.add(userTextField);
        //密码标签
        passWordLabel = new JLabel("密  码：");       //定义标签对象
        passWordLabel.setBounds(52 + 280, 74 + 130, 54, 15);
        root.add(passWordLabel);
        passWordLabel.setFont(centerFont);
        //密码文本框
        passWordTextField = new JPasswordField(12);
        passWordTextField.setBounds(116 + 280, 71 + 130, 139, 21);
        passWordTextField.setEchoChar('●');       //设置回显字符
        root.add(passWordTextField);

     rdbtnNewRadioButton = new JRadioButton("学生");
        rdbtnNewRadioButton.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 15));
        rdbtnNewRadioButton.setContentAreaFilled(false);
        rdbtnNewRadioButton.setBorderPainted(false);
        rdbtnNewRadioButton.setBounds(320, 230, 80, 20);
        root.add(rdbtnNewRadioButton);

      rdbtnNewRadioButton_1 = new JRadioButton("教师");
        rdbtnNewRadioButton_1.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 15));
        rdbtnNewRadioButton_1.setContentAreaFilled(false);
        rdbtnNewRadioButton_1.setBorderPainted(false);
        rdbtnNewRadioButton_1.setBounds(320 + 80, 230, 80, 20);
        rdbtnNewRadioButton.setBorderPainted(false);
        rdbtnNewRadioButton.setFocusPainted(false);
        root.add(rdbtnNewRadioButton_1);

         rdbtnNewRadioButton_2 = new JRadioButton("管理员");
        rdbtnNewRadioButton_2.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 15));
        rdbtnNewRadioButton_2.setContentAreaFilled(false);
        rdbtnNewRadioButton_2.setBorderPainted(false);
        rdbtnNewRadioButton_2.setBounds(320 + 80 + 80, 230, 80, 20);
        rdbtnNewRadioButton.setBorderPainted(false);
        rdbtnNewRadioButton.setFocusPainted(false);
        root.add(rdbtnNewRadioButton_2);
        //登录按钮
        enterButton = new JButton("登录");//定义按钮对象
        enterButton.setBackground(Color.LIGHT_GRAY);
        enterButton.setBounds(64 + 350, 116 + 130 + 30, 69, 23);
        root.add(enterButton);


        //设置窗口风格
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    private void initimage() {
        //添加登录主界面
        ImageIcon imageIcon = new ImageIcon("imge\\Login1.2.png");
        JLabel label = new JLabel(imageIcon);
        root.add(label, BorderLayout.CENTER);
        //修改图标
        Toolkit tk = Toolkit.getDefaultToolkit();
        //获取图片 三种图片格式都可以
        java.awt.Image img = tk.getImage("imge\\成绩录入2.png");
        this.setIconImage(img);//详细分析

        setBounds(350, 130, 600, 450);
    }



}



